function formatBytes(bytes, decimals = 2) {
  if (bytes <= 0) {
    return "0B";
  }

  const base = 1024;
  const dm = Math.max(decimals, 0);
  const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  const i = Math.floor(Math.log(bytes) / Math.log(base));
  return parseFloat((bytes / Math.pow(base, i)).toFixed(dm)) + units[i];
}

module.exports = {
  formatBytes,
};

if (require.main === module) {
  console.log(formatBytes(-125));
  console.log(formatBytes(0));
  console.log(formatBytes(644));
  console.log(formatBytes(2362));
  console.log(formatBytes(458654252));
  console.log(formatBytes(89458654252));
}
